/*
 * 文件名：Page.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-25
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

public class Page
{
    private static final int MOD = 2;
   
    // 每页显示的条数
    private int numPerPage = Constants.INT_NUMPERPAGE;

    // 当前页,初始化为1,即默认从第一页开始
    private int curPage = 1;

    // 起始数据
    private int firstResult;

    // 总条数
    private int totalNum;

    // 总页数
    private int totalPage;

    // 排序的顺序
    private int order;

    // 排序的字段
    private String orderBy;

    //页面checkbox值
    private String tabchildcheckboxString;

    private int displayCount = Constants.INT_PAGE_FLAG; 
    
    private int show = 0;

    
    public String toString()
    {
        return new ToStringBuilder(this).append("numPerPage", numPerPage)
                .append("curPage", curPage).append("firstResult", firstResult)
                .append("totalNum", totalNum).append("totalPage", totalPage)
                .append("order", order).append("orderBy", orderBy).toString();
    }

   /**
    * 获得起始始记录
    * Description: <br>
    * Implement: <br>
    * @return
    * @see
    * @return int 起始记录
    * @author ozm
    */
    public int getFirstResult()
    {
        // 当前页小于1
        if (this.curPage < 1)
        {
            this.curPage = 1;
        }
        else if (this.curPage > this.getTotalPage())
        {
            // 传入的页数大于总页数
            this.curPage = totalPage;
        }
        this.firstResult = (this.curPage - 1) * this.numPerPage;
        return firstResult;
    }

    public int getCurPage()
    {
        //如果当前页大于总页数，则返回的当前页是总页数
        return curPage;
    }

    public void setCurPage(int curPage)
    {
        this.curPage = curPage;
    }

    public int getNumPerPage()
    {
        return numPerPage;
    }

    public void setNumPerPage(int numPerPage)
    {
        this.numPerPage = numPerPage;
    }

    public void setTotalNum(int totalNum)
    {
        this.totalNum = totalNum;
    }

    public int getTotalNum()
    {
        return totalNum;
    }
    
    /**
    * 
    * Description: 获得查询总页数
    * Implement: <br>
    * @see
    * @return int 总页数
    * @author ozm2013-1-28下午2:16:44
    */
    public int getTotalPage()
    {
        this.totalPage = (this.totalNum / this.numPerPage)
                + (0 == (this.totalNum % this.numPerPage) ? 0 : 1);
        return totalPage;
    }

    /**
     * 
     * Description: 页下标
     * Implement: <br>
     * @see
     * @return List lsit
     * @author ozm2013-1-28下午2:17:14
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public List getList()
    {
        
        int n = (int)displayCount / MOD;
        
        int curPageNum = this.curPage;
        List list = new ArrayList();
//        boolean p = true;
        
        if (totalPage > displayCount)
        {
            
            if (totalPage - curPage < n)
            {
                n = n + (n + curPage - totalPage);
            }
            int i = n;
            for (; i > 0; i--)
            {
                if ((curPageNum - i) < 0)
                {
                    break;
                }
                else if (curPageNum == i)
                {
                    continue;
                }
                
                list.add(curPageNum - i);
            }
            
            for (int j = curPageNum; j <= totalPage && list.size() < displayCount; j++)
            {
                list.add(j);
            }
            
        }
        else
        {
            for (int i = 1; i <= totalPage; i++)
            {
                list.add(i);
            }
        }
        
        return list;
    }

    public void setTotalPage(int totalPage)
    {
        this.totalPage = totalPage;
    }

    public int getOrder()
    {
        return order;
    }

    public void setOrder(int order)
    {
        this.order = order;
    }

    public String getOrderBy()
    {
        return orderBy;
    }

    public void setOrderBy(String orderBy)
    {
        this.orderBy = orderBy;
    }


    public String getTabchildcheckboxString()
    {
        return tabchildcheckboxString;
    }

    public void setTabchildcheckboxString(String tabchildcheckboxString)
    {
        this.tabchildcheckboxString = tabchildcheckboxString;
    }

    public int getShow()
    {
        return show;
    }

    public void setShow(int show)
    {
        this.show = show;
    }
}
